package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.TopBar;
import com.telecom.weixi.R;

public class MyAchiverment_Income extends LoadingMoreAbstractList {
	private CstInfoAdapter adapter;

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		getWebServiceData(WeixiApplication.getUserInfo().getTicket(), Which.MY_ACHIVERMENT, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, PAGE_SIZE, currentPage);
	}

	@Override
	public int getLayout() {
		return R.layout.bu_common_list_my_achiver_income;
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new CstInfoAdapter(context);
		return adapter;
	}

	@Override
	public Object paserData(SoapObject retMsgObject, Object... params) {
		List<CstInfo> tems = new ArrayList<MyAchiverment_Income.CstInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				CstInfo cstInfo = new CstInfo();
				cstInfo.cust_manager_name = taskBeanObject.getPropertySafelyAsString("cust_manager_name");// 客户类型
				cstInfo.cur_mon = taskBeanObject.getPropertySafelyAsString("cur_mon");// 客户ARPU
				cstInfo.lst_mon = taskBeanObject.getPropertySafelyAsString("lst_mon");// 产品信息
				cstInfo.l2st_mon = taskBeanObject.getPropertySafelyAsString("l2st_mon");// 收入信息
				tems.add(cstInfo);
			}
		}
		return tems;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Message msg) {
		List<CstInfo> tems = (List<CstInfo>) msg.obj;
		if (null != msg.getData()) {
			Bundle bundle = msg.getData();
			((TextView) findViewById(R.id.cust_manager_name)).setText(bundle.getString("cust_manager_name"));
			((TextView) findViewById(R.id.cur_mon)).setText(bundle.getString("cur_mon"));
			((TextView) findViewById(R.id.lst_mon)).setText(bundle.getString("lst_mon"));
			((TextView) findViewById(R.id.l2st_mon)).setText(bundle.getString("l2st_mon"));
		}
		empytText.setVisibility(View.GONE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				refreshListView.removeFooterView(loadMoreView);
			}
		}
		adapter.setCstInfos(tems);
		adapter.notifyDataSetChanged();
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}
	}

	class CstInfoAdapter extends BaseAdapter {

		List<CstInfo> custInfos = new ArrayList<CstInfo>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public CstInfoAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setCstInfos(List<CstInfo> custInfos) {
			if (currentPage == 1) {
				this.custInfos.clear();
			}
			this.custInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return custInfos.size();
		}

		@Override
		public CstInfo getItem(int position) {
			return custInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			CstInfo custInfo = custInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item_my_achiver_income, null);
				holder = new ViewHolder();
				holder.cust_manager_name = (TextView) convertView.findViewById(R.id.cust_manager_name);
				holder.cur_mon = (TextView) convertView.findViewById(R.id.cur_mon);
				holder.lst_mon = (TextView) convertView.findViewById(R.id.lst_mon);
				holder.l2st_mon = (TextView) convertView.findViewById(R.id.l2st_mon);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.cust_manager_name.setText(custInfo.cust_manager_name);
			holder.cur_mon.setText(custInfo.cur_mon);
			holder.lst_mon.setText(custInfo.lst_mon);
			holder.l2st_mon.setText(custInfo.l2st_mon);

			int resid = R.drawable.table_row1;
			if (position % 2 == 0) {
				resid = R.drawable.table_row0;
			}
			holder.cur_mon.setBackgroundResource(resid);
			holder.lst_mon.setBackgroundResource(resid);
			holder.l2st_mon.setBackgroundResource(resid);
			return convertView;
		}

		class ViewHolder {
			TextView cust_manager_name;// 客户类型
			TextView cur_mon;// 客户ARPU
			TextView lst_mon;// 产品信息
			TextView l2st_mon;// 收入信息
		}

	}

	class CstInfo {
		String cust_manager_name;// 客户经理
		String cur_mon;// 当月收入
		String lst_mon;// 上月收入
		String l2st_mon;// 上上月收入

	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindBtn();
		topBarReturn(topBar);
		topBar.setTitle(title);
	}

	@Override
	public String getMethod() {
		return "getIncomePage";
	}

}
